﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL;

namespace BO.Admin
{
    public class News_Helper
    {
        public static DataTable GetByCategory(int Category_ID)
        {
            DataTable dt;
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.News_GetByCategory(Category_ID);
            }
            if (!dt.Columns.Contains("NewsUrl")) dt.Columns.Add("NewsUrl");
            if (!dt.Columns.Contains("NewsPhoto")) dt.Columns.Add("NewsPhoto");
            foreach (DataRow row in dt.Rows)
            {
                row["NewsUrl"] = Utils.BuildNewsUrl(row["Category_DisplayUrl"].ToString(), Convert.ToInt32(row["News_ID"]), row["News_Title"].ToString());
                row["NewsPhoto"] = Utils.BuidImageUrl(row["News_Photo"].ToString());
            }
            return dt;
        }
        public static int AddDump(string News_Title)
        {
            DataTable dt;
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.News_AddDump(News_Title);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                int result = 0;
                try
                {
                    result = Convert.ToInt32(dt.Rows[0][0]);
                }
                catch { }
                return result;
            }
            else return 0;
        }
        public static DataTable GetByNewsID(int News_ID)
        {
            DataTable dt;
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.News_GetByNewsID(News_ID);
            }
            return dt;
        }
        public static void Update(int News_ID, int Category_ID, string News_Title, string Photo, string Stylist, string Makeup, string Model,
            string Costume, string Location, int News_Status, string News_Photo)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.News_Update(News_ID, Category_ID, News_Title, Photo, Stylist, Makeup, Model, Costume, Location,
                    News_Status, News_Photo);
            }
        }
        public static void UpdateStatus(int News_ID, int News_Status)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.News_UpdateStatus(News_ID, News_Status);
            }
        }
        public static DataTable GetTopAll(int Top)
        {
            DataTable dt;
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.News_GetTopAll(Top);
            }
            if (!dt.Columns.Contains("StatusText")) dt.Columns.Add("StatusText");
            if (!dt.Columns.Contains("CreatedDateText")) dt.Columns.Add("CreatedDateText");
            foreach (DataRow row in dt.Rows)
            {
                row["StatusText"] = GetStatusText(row["News_Status"]);
                row["CreatedDateText"] = Convert.ToDateTime(row["News_CreatedDate"]).ToString(PublicConstants.VietnameseShortDateFormat);
            }
            return dt;
        }
        public static string GetStatusText(object Status)
        {
            int status = 0;
            try
            {
                status = Convert.ToInt32(Status);
            }
            catch { }
            switch (status)
            {
                case (int)News.Status.Draft: return "Nháp";
                case (int)News.Status.Published: return "Xuất bản";
                case (int)News.Status.Deleted: return "Đã xóa";
                default: return "Nháp"; 
            }
        }
        public static string GetDumpTitle()
        {
            return "Bộ ảnh ngày " + DateTime.Now.ToString(PublicConstants.VietnameseShortDateFormat);
        }
        public static DataTable GetHighView(int Top)
        {
            DataTable dt;
            using (MainDB db = new MainDB())
            {
                dt = db.StoredProcedures.News_GetHighView(Top);
            }
            return dt;
        }
    }
}
